package com.example.m_kartupay;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast; 
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.m_kartupay.LoginActivity;
import com.example.m_kartupay.library.InquiryDatabaseHandler;
import com.example.m_kartupay.library.StrictWrapper;
import com.example.m_kartupay.library.UserFunctions;
import com.example.m_kartupay.library.DatabaseHandler;

public class MainMenuActivity extends Activity {
	String account_number = null;
	String balance = null;
	String points = null;
	UserFunctions userFunctions;
	HashMap<String, String> account; 
	TextView myAccount;
	TextView accountNo;
	 ListView list;
	 String[] menu = {
			      "My Account",
			      "My Transaction",
			      "Top Up",
			      "Transfer",
			      "Payment",
			      "Purchase",
			      "Settings",
			      "Help",
			      "Log Out"
     } ;
	 Integer[] imageId = {
			  R.drawable.ic_edit,
		      R.drawable.ic_calender,
		      R.drawable.ic_cloud,
		      R.drawable.ic_transfer,
		      R.drawable.ic_money,
		      R.drawable.ic_purchase,
		      R.drawable.ic_setting,
		      R.drawable.ic_information,
		      R.drawable.ic_logout,
			
		  };
	 @Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
		StrictWrapper.init();
		
	     userFunctions = new UserFunctions();
	   
	     DatabaseHandler db = new DatabaseHandler(getApplicationContext());
	 	 account = db.getUserDetails();
	 	 
	 	 String channel_id = "123456789";
	 	 String email = (String)account.get("email");
	 	 String user_id = (String)account.get("user_id");
		
		 JSONObject jsonInquiry = userFunctions.inquiry(channel_id, user_id, email);
		
		 Log.d("JSON INQUIRY",jsonInquiry.toString());
		 
		 InquiryDatabaseHandler dbInquiry = new InquiryDatabaseHandler(getApplicationContext());
		  String res;
			try {
				res = jsonInquiry.getString("responseCode");
				if(Integer.parseInt(res) ==0)
				  {
					 JSONObject object = jsonInquiry.getJSONObject("data");
					 account_number = object.getString("account_number");
					 balance = object.getString("balance");
					 points = object.getString("points");
				  }
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		  
		 
	     
		
			if(userFunctions.isUserLoggedIn(getApplicationContext())){
				Log.d("Panggil Activity","MainMenuActivity karena loging");
				setContentView(R.layout.main_menu);
				
				// Set Text View
				myAccount = (TextView)findViewById(R.id.myAccount);
				accountNo = (TextView)findViewById(R.id.accountNo);
			
				
				myAccount.setText((String)account.get("name"));
				accountNo.setText((String)account.get("user_id"));
				
		    	 ListMenuActivity adapter = new
		 	    		ListMenuActivity(MainMenuActivity.this, menu, imageId);
		 	    list=(ListView)findViewById(R.id.list);
		 	        list.setAdapter(adapter);
		 	        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		 	                @Override
		 	                public void onItemClick(AdapterView<?> parent, View view,
		 	                                        int position, long id) {
		 	                    Toast.makeText(MainMenuActivity.this, "You Clicked at " +menu[+ position], Toast.LENGTH_SHORT).show();
		 	                    String _menu = menu[+position];
		 	                   // Toast.makeText(MainMenuActivity.this, "You Clicked at " +menu[+ position], Toast.LENGTH_SHORT).show();
		 	                    
		 	                    // Buat Switch untuk pemilihan intent
		 	                    
		 	                   switch(_menu)
		 	                   {
		 		                   case "My Account":
		 		                	  Intent myaccount =  new Intent(view.getContext(), MyAccountActivity.class);
		 		                	  myaccount.putExtra("account_number",account_number);
		 		                	  myaccount.putExtra("balance",balance);
		 		                	  myaccount.putExtra("points",points); 
		 		                	  startActivity(myaccount);
		 		                	   break;
		 		                	 
		 		                   case "My Transaction":
		 		                	  Intent mytransaction1 =  new Intent(view.getContext(), MainListActivity.class);
		 		                	   
		 		                	   mytransaction1.putExtra("account_number",account_number);
			 		                   mytransaction1.putExtra("balance",balance);
			 		                   mytransaction1.putExtra("points",points);
			 		                   startActivity(mytransaction1);
		 		                	   break;	 		                	   
		 		                   case "Top Up":
		 		                	  break;
		 		                   case "Transfer":
		 		                	   Intent in =  new Intent(view.getContext(), TransferActivity.class);
		 		                	   startActivity(in);
		 		                	   break;
		 		                   case "Payment":		 		                	  
		 		                	   Intent payment =  new Intent(view.getContext(), PaymentActivity.class);
		 		                	   startActivity(payment);
		 		                	   
		 		                	   break;
		 		                   case "Purchase":
		 		                	   Intent purchase =  new Intent(view.getContext(), PurchaseActivity.class);
		 		                	   startActivity(purchase);
		 		                	   break;
		 		                   case "Settings":
		 		                	   Intent setting =  new Intent(view.getContext(), SettingActivity.class);
		 		                	   startActivity(setting);
		 		                	   break;		 		                	   
		 		                   case "Help":
		 		                	   Intent help =  new Intent(view.getContext(), HelpActivity.class);
		 		                	   startActivity(help);
		 		                	   break;		 		                   
		 		                   case "Log Out":
		 		                	   userFunctions.logoutUser(getApplicationContext());
			 		     				Intent login = new Intent(getApplicationContext(), LoginActivity.class);
			 		     	        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			 		     	        	startActivity(login);
			 		     	        	// Closing dashboard screen
			 		     	        	finish();
		 		                	   break;
		 	                   }                
		 	                 	                  
		 	                	 	    			    
		 	                
		 	                }
		 	            });


				
			}
			else
			{
			   // local test	
				/*
				Log.d("Panggil Activity","MainMenuActivity karena loging");
				setContentView(R.layout.main_menu);
			
				// Set Text View
				myAccount = (TextView)findViewById(R.id.myAccount);
				accountNo = (TextView)findViewById(R.id.accountNo);
				HashMap<String, String> account = db.getUserDetails();
				
				myAccount.setText((String)account.get("name"));
				accountNo.setText((String)account.get("user_id"));
				
		    	 ListMenuActivity adapter = new
		 	    		ListMenuActivity(MainMenuActivity.this, menu, imageId);
		 	    list=(ListView)findViewById(R.id.list);
		 	        list.setAdapter(adapter);
		 	        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		 	                @Override
		 	                public void onItemClick(AdapterView<?> parent, View view,
		 	                                        int position, long id) {
		 	                    Toast.makeText(MainMenuActivity.this, "You Clicked at " +menu[+ position], Toast.LENGTH_SHORT).show();
		 	                    String _menu = menu[+position];
		 	                   // Toast.makeText(MainMenuActivity.this, "You Clicked at " +menu[+ position], Toast.LENGTH_SHORT).show();
		 	                    
		 	                    // Buat Switch untuk pemilihan intent
		 	                    
		 	                   switch(_menu)
		 	                   {
		 		                   case "My Account":
		 		                   Intent myaccount =  new Intent(view.getContext(), MyAccountActivity.class);
		 		                	   startActivity(myaccount);
		 		                	   break;
		 		                	
		 		                   case "My Transaction":
		 		                	   Intent mytransaction1 =  new Intent(view.getContext(), MainListActivity.class);
		 		                	   startActivity(mytransaction1);
		 		                	   break;
		 		                	   
		 		                   case "Top Up":
		 		                	   break;
		 		                   case "Transfer":
		 		                	   Intent in =  new Intent(view.getContext(), TransferActivity.class);
		 		                	   startActivity(in);
		 		                	   break;
		 		                   case "Payment":
		 		                	   Intent payment =  new Intent(view.getContext(), PaymentActivity.class);
		 		                	   startActivity(payment);
		 		                	   break;
		 		                   case "Purchase":
		 		                	   Intent purchase =  new Intent(view.getContext(), PurchaseActivity.class);
		 		                	   startActivity(purchase);
		 		                	   break;
		 		                   case "Settings":
		 		                	  Intent setting =  new Intent(view.getContext(), SettingActivity.class);
		 		                	   startActivity(setting);
		 		                	   break;		 		  
		 		                	   
		 		                   case "Help":
		 		                	   Intent help =  new Intent(view.getContext(), HelpActivity.class);
		 		                	   startActivity(help);
		 		                	   break;
		 		                   
		 		                   case "Log Out":
		 		                	   userFunctions.logoutUser(getApplicationContext());
			 		     				Intent login = new Intent(getApplicationContext(), LoginActivity.class);
			 		     	        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			 		     	        	startActivity(login);
			 		     	        	// Closing dashboard screen
			 		     	        	finish();
		 		                	   break;
		 	                   }                
		 	                 	                  
		 	                	 	    			    
		 	                
		 	                }
		 	            });
		 	        */
		 	           
				// debug online
			    
				Log.d("Panggil Activity","Login karena belum login");
				// user is not logged in show login screen
	        	Intent login = new Intent(getApplicationContext(), LoginActivity.class);
	        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        	startActivity(login);
	        	// Closing dashboard screen
	        	finish();
	        	
	        
			  
			}
		
	    }
}
